package com.qf.quduba.controller;

import com.qf.quduba.VO.ResultVO;
import com.qf.quduba.entity.Comment;
import com.qf.quduba.service.CommentService;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 作者：程意
 * 时间：2020/10/16 11:01
 */
@RestController
@CrossOrigin
@RequestMapping("/comment")
public class CommentController {
    @Resource
    private CommentService commentService;
    @GetMapping("findComment")
    public ResultVO findCommentByBookId(String bookId){
        List<Comment> commentList = commentService.findCommentByBookId(bookId);
        if(commentList != null && commentList.size()>0){
            return new ResultVO(1000,"成功",commentList);
        }else{
            return new ResultVO(1001,"失败",null);
        }
    }

    @GetMapping("findCommentOne")
    public ResultVO findCommentOne(Integer commentId){
        Comment commentOne = commentService.findCommentOne(commentId);
        if(commentOne != null){
            return new ResultVO(1000,"成功",commentOne);
        }else{
            return new ResultVO(1001,"失败",null);
        }
    }

    @DeleteMapping("deleteComment")
    public ResultVO deleteCommentByCommentId(Integer userId,String commentId){
        Comment comment = commentService.findCommentOne(Integer.parseInt(commentId));

        if(userId==(comment.getUserId())){
            int i = commentService.deleteComment(commentId);
            if(i != 0){
                return new ResultVO(1000,"成功",null);
            }else{
                return new ResultVO(1001,"失败",null);
            }
        }else {
            return new ResultVO(1001,"您不能删除此评论",null);
        }
    }

    @PostMapping("addComment")
    public ResultVO addComment(String commentDetail,String bookId,String userId){
        int i = commentService.saveComment(commentDetail,bookId,userId);
        if(i != 0){
            return new ResultVO(1000,"成功",null);
        }else{
            return new ResultVO(1001,"失败",null);
        }
    }
    @PutMapping("updateComment")
    public ResultVO updateComment(String commentId){
        int i = commentService.updateCommentNum(commentId);
        if(i != 0){
            return new ResultVO(1000,"成功",null);
        }else{
            return new ResultVO(1001,"失败",null);
        }
    }
    @GetMapping("findByBookId")
    public List<Comment> findByGoodId( Integer bookId) {
        List<Comment> allComments = commentService.findByBookId(bookId);
        if (allComments == null || allComments.size() == 0) {
            return new ArrayList<>();
        }
        List<Comment> comments = new ArrayList<>();
        List<Comment> parents = new ArrayList<>();
        for (Comment comment : allComments) {
            if (comment.getParentId()==null) {
                comments.add(comment);
                parents.add(comment);
            } else {
                boolean foundParent=false;
                for (Comment parent : parents) {
                    if (comment.getParentId() == parent.getCommentId()) {
                        if (parent.getChild() == null) {
                            parent.setChild(new ArrayList<>());
                        }
                        parent.getChild().add(comment);
                        parents.add(comment);
                        foundParent=true;
                        //如果对list迭代过程中同时修改list，会报java.util.ConcurrentModificationException
                        // 的异常，所以我们需要break,当然break也可以提高算法效率
                        break;
                    }
                }
                if (!foundParent) {
                    throw new RuntimeException("can not find the parent comment");
                }
            }
        }
        return comments;
    }





}
